package javass;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

public class Fenyeservlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public Fenyeservlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json; charset=utf-8");
		request.setCharacterEncoding("UTF-8");
		ConnectionUtil connutil=new ConnectionUtil();
		Connection conn;
		try {
			conn = connutil.getConn();
			PreparedStatementUtil psu=new PreparedStatementUtil();
			ResultSetUtil rsu=new ResultSetUtil(); 
			String sql0=" SELECT COUNT(v1.title)FROM v1; ";
			PreparedStatement ps0=psu.getPreparedStatement(conn, sql0);
			ResultSet rs0=rsu.getResultSet(ps0);
			int count=0;
			if(rs0.next()){
				count= rs0.getInt(1);
			}
			final int pagesize=2;//每页显示条数
			int pagecount=0;//总页数
			int current=Integer.valueOf(request.getParameter("current"));//获取当前页数
			
			 if(count%pagesize==0){//如果能整除
				 	  pagecount=count/pagesize;
			      }else{
			    	  pagecount=(int)(count/pagesize)+1;
			      }
			String sql=" select * from v1 limit ?,? ";
			PreparedStatement ps=psu.getPreparedStatement(conn, sql);
			ps.setInt(1, current*pagesize);
			ps.setInt(2, pagesize);
			ResultSet rs=rsu.getResultSet(ps);
			Map<String,Object> map;
			List<Map<String,Object>> l=new ArrayList<Map<String,Object>>();
			while(rs.next()){
				map=new HashMap<String,Object>();
				map.put("title", rs.getString(1));
				map.put("auto", rs.getString(2));
				map.put("date", rs.getString(3));
				l.add(map);
			}
			PrintWriter out = response.getWriter();
			out.print(JSON.toJSON(l));
			out.flush();
			out.close();
	
			} catch (ClassNotFoundException | SQLException e) {
				e.printStackTrace();
			}
	}
	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
		
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
